package com.itany.controller;

import com.github.pagehelper.PageInfo;
import com.itany.pojo.User;
import com.itany.service.UserService;
import com.itany.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @GetMapping("/findAll")
    @ResponseBody
    public Map<String, Object> findAll(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                       @RequestParam(name = "rows", defaultValue = "5") Integer rows,
                                       String username, Integer flag) {
        Map<String, Object> map = new HashMap<String, Object>();
        PageInfo<User> info = userService.findUserByExample(page, rows, username, flag);
        map.put("total", info.getTotal());
        map.put("rows", info.getList());
        return map;
    }


    /**
     * 修改状态
     *
     * @param id
     * @param flag
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public ActionResult update(@RequestParam(name = "id", defaultValue = "1") Integer id,
                               @RequestParam(name = "flag") Integer flag) {

        ActionResult result = new ActionResult();
        userService.updateStatus(id, flag);
        result.setStatus(200);
        result.setMsg("成功");
        return result;
    }


    @RequestMapping("findOne")
    @ResponseBody
    public ActionResult findOne(@RequestParam(name = "id", defaultValue = "1") Integer id) {

        ActionResult result = new ActionResult();
        User user = userService.findOne(id);

        result.setStatus(200);
        result.setMsg("成功");
        result.setData(user);
        return result;
    }


    /**
     * 修改user
     *
     * @param id
     * @param username
     * @param phone
     * @param email
     * @param interest
     * @return
     */
    @RequestMapping("modify")
    @ResponseBody
    public ActionResult modify(@RequestParam(name = "id", defaultValue = "1") Integer id,
                               String username, String phone, String email, String interest) {

        ActionResult result = new ActionResult();
        userService.modify(id, username, phone, email, interest);
        result.setStatus(200);
        result.setMsg("成功");
        return result;
    }


}
